package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.mapper.SignatureMapper;
import com.example.blog.pojo.Result;
import com.example.blog.pojo.Signature;
import com.example.blog.service.SignatureService;
import com.example.blog.util.TokenUtil.decryptToken;
import com.example.blog.util.TokenUtil.spiltUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class SignatureServiceImpl extends ServiceImpl<SignatureMapper, Signature> implements SignatureService {
    @Resource
    private SignatureMapper signatureMapper;

    /***
     * 添加个人随笔
     * @param signature
     * @param token
     * @return
     */
    @Override
    public Result<T> saveSignature(Signature signature, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        signature.setName(userName);
        int insert = signatureMapper.insert(signature);
        if (insert > 0) {
            return Result.success("添加成功！");
        }
        return Result.failed("添加失败");
    }

    /***
     * 获取用户的个人随笔
     * @param token
     * @return
     */
    @Override
    public Result<List<Signature>> getUserSignature(String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        QueryWrapper<Signature> wrapper = new QueryWrapper<>();
        wrapper.eq("name", userName);
        List<Signature> list = signatureMapper.selectList(wrapper);
        return Result.success(list);
    }

    /***
     * 删除用户个人随笔
     * @param id
     * @return
     */
    @Override
    public Result<T> deleteSignatureRecord(int id) {
        int delete = signatureMapper.deleteById(id);
        if (delete > 0) {
            return Result.success("删除成功！");
        }
        return Result.failed("删除失败");
    }
}
